import 'dart:collection';
import 'dart:convert';
import 'dart:ui';

import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_theme/constant.dart';
import 'package:flutter_theme/theme_utils.dart';

class WXTheme {
  bool _isFollowSystem = false;

  String moduleName = "";

  String _themeType = "";

  String _systemThemeType = "";

  static Map<String, dynamic> skinMap = Map();

  static Map<String, dynamic> basicColorMap = Map();

  static List<String> themeAllColors = List();

  WXTheme._internal();

  static WXTheme _singleton = WXTheme._internal();

  factory WXTheme() => _singleton;

  void setFollowSystem(bool isFollowSystem) {
    this._isFollowSystem = isFollowSystem;
  }

  bool get isFollowSystem => _isFollowSystem;

  void setThemeType(String themeType) {
    this._themeType = themeType;
  }

  String get themeType => _themeType;

  void setSystemThemeType(String systemThemeType) {
    this._systemThemeType = systemThemeType;
  }

  String get systemThemeType => _systemThemeType;

  void init({@required List<String> paths, String basicColorPath, String themeType = "system"}) async {
    if (themeType == "system") {
      setFollowSystem(true);
      if (window.platformBrightness == Brightness.light) {
        themeType = "light";
      } else {
        themeType = "dark";
      }
    }else{
      setFollowSystem(false);
    }
    setThemeType(themeType);
    if (themeAllColors.isEmpty) {
      /// 解析基准颜色json文件
      String basicColor = await rootBundle.loadString(basicColorPath);
      basicColorMap = json.decode(basicColor);
      print("basicColorMap=======>$basicColorMap");

      paths.forEach((path) async {
        /// 解析子模块json文件得到文件内容
        String themeColor = await rootBundle.loadString(path);
        String module = _splitPath(path);
        print("themeColor========>$themeColor========>$module");
        skinMap[module] = themeColor;
        print("themeColor===map=====>$skinMap");
        //  SpUtil.putString(module, themeColor);
        themeAllColors.add(themeColor);
      });
    }
  }

  String _splitPath(String path) {
    if (path.isEmpty) return "";
    if (path.contains("/")) {
      List<String> list = path.split("/");
      if (path.startsWith("packages")) {
        return list[1] ?? "";
      } else {
        return list[0] ?? "";
      }
    }
    return "";
  }

//  static Color $c(BuildContext context,String key){
////    if(ThemeUtils.isDark(context)){
////      WXTheme().setThemeType("dark");
////    }else{
////      WXTheme().setThemeType("light");
////    }
//    return _getColor(key);
//  }

  /// 单色配置
  static Color $c(String key, {String packageName}) {
    if (packageName == null || packageName.isEmpty) {
      Color themeColor = Colors.red;
      themeAllColors.forEach((themeColors) {
        Map<String, dynamic> themeMap = json.decode(themeColors)[WXTheme().themeType];

        /// 处理Map间映射
        themeMap.updateAll((key, value) {
          if (basicColorMap.containsKey(value)) {
            return themeMap[key] = basicColorMap[value];
          }
        });
        if (themeMap.containsKey(key)) {
          String color = themeMap[key];
          if (color.startsWith("0x") && color.length >= 8) {
            themeColor = Color(int.parse(color));
            return;
          }
        }
      });
      return themeColor;
    } else {
      /// 传了packagesName的情况，把skin.json和基准colors.json的内容转化成map形式
      String themeColors = skinMap[packageName];
      Map<String, dynamic> themeMap = json.decode(themeColors)[WXTheme().themeType];

      /// 处理Map间映射
      themeMap.updateAll((key, value) {
        if (basicColorMap.containsKey(value)) {
          return themeMap[key] = basicColorMap[value];
        }
      });
      if (themeMap.containsKey(key)) {
        String color = themeMap[key];
        if (color.startsWith("0x") && color.length >= 8) {
          return Color(int.parse(color));
        } else {
          return Color(0xFFFFFFFF);
        }
      }
    }
    return Color(0xFFFFFFFF);
  }

  /// 渐近色
  static List<Color> $g(List<String> colors) {
    if (colors.length <= 0) return [];
    List<Color> colorList = [];
    colors.forEach((color) {
      colorList.add($c(color));
    });
    return colorList;
  }
}
